package org.chenbingkang.esper.client;

import com.espertech.esper.client.EPAdministrator;
import com.espertech.esper.client.EPServiceProvider;
import com.espertech.esper.client.EPServiceProviderManager;
import com.espertech.esper.client.EPStatement;
import org.chenbingkang.esper.listener.TeamEventListener;

import java.util.HashMap;
import java.util.Map;

public class EsperClient2 {

    private EPServiceProvider engine;

    public EsperClient2() {
        //obtain an engine instance
        this.engine = EPServiceProviderManager.getDefaultProvider();
        //System.out.println(engine.getURI());
    }

    public void personEventProcess() {
        EPAdministrator admin = engine.getEPAdministrator();
        Map<String, Object> coder = new HashMap<String, Object>();
        coder.put("name", String.class);
        coder.put("salary", int.class);
        coder.put("teamName", String.class);

        Map<String, Object> team = new HashMap<String, Object>();
        team.put("teamName", String.class);
        team.put("totalSalary", int.class);
        admin.getConfiguration().addEventType("Coder", coder);
        admin.getConfiguration().addEventType("Team", team);

        String epl4CoderIn = "insert Team(teamName, totalSalary) select teamName,sum(salary) as totalSalary from Coder group by teamName";
        String epl4TeamIn = "select teamName, totalSalary from Team";

        EPStatement state = admin.createEPL(epl4CoderIn);
         state = admin.createEPL(epl4TeamIn);
        state.addListener(new TeamEventListener());

    }


    public void send(Map map) {

        engine.getEPRuntime().sendEvent(map,"Coder");

    }
}
